/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafx191labs.agordeev.game.data;



/**
 *
 * @author r4diantq
 */
public class gameMatrix {
    //public gameNode[][] mtx = new gameFreeBlock[21][21];

    public ingame2Darr mtx = new ingame2Darr();
    public gameMatrix(){
        setExitRows();
        for(int i = 0; i < 21; i++){
            for(int j = 0; j < 21; j++){
                //mtx[i][j] = new gameFreeBlock();
                mtx.row[i].column[j].single = new gameFreeBlock();
            }
        }
    }
    public void setChiken(int row, int col){
        //this.mtx[row][col] = new gameChiken();
        mtx.row[row].column[col].single = new gameChiken();
    }
    public void setBlocks(int row, int col){
        //this.mtx[row][col] = new gameBlocksUser();
        mtx.row[row].column[col].single = new gameBlocksUser();
    }
    public void setExitRows(){
        for(int i = 0; i < 21; i++){
            //this.mtx[0][i] = new gameExitBlock();
            mtx.row[0].column[i].single = new gameExitBlock();
        }
        for(int i = 0; i < 21; i++){
            //this.mtx[20][i] = new gameExitBlock();
            mtx.row[20].column[i].single = new gameExitBlock();
        }
    }
    public void CalcilateApproachibility(){
        int arr3[] = new int[3];
        int arr4[] = new int[4];
        for(int i = 0; i < 21; i++){
            for(int j = 0; j < 21; j++){/*
                if(this.mtx[i][j].getName().equals("E")){
                    this.mtx[i][j].setApproachibility(1);
                } else if(this.mtx[i][j].getName().equals("F")){
                    if(j-1 == -1){
                        arr3[0] = this.mtx[i-1][j].getApproachibility();
                        arr3[1] = this.mtx[i+1][j].getApproachibility();
                        arr3[2] = this.mtx[i][j+1].getApproachibility();
                        this.mtx[i][j].setApproachibility(getMinValue(arr3) + 1);
                    } else if(j+1 == 21){
                        arr3[0] = this.mtx[i-1][j].getApproachibility();
                        arr3[1] = this.mtx[i+1][j].getApproachibility();
                        arr3[2] = this.mtx[i][j-1].getApproachibility();
                        this.mtx[i][j].setApproachibility(getMinValue(arr3) + 1);
                    } else{
                        arr4[0] = this.mtx[i-1][j].getApproachibility();
                        arr4[1] = this.mtx[i+1][j].getApproachibility();
                        arr4[2] = this.mtx[i][j+1].getApproachibility();
                        arr4[3] = this.mtx[i][j-1].getApproachibility();
                        this.mtx[i][j].setApproachibility(getMinValue(arr4) + 1);
                    }
                } else if(this.mtx[i][j].getName().equals("C")){

                }*/
                if(this.mtx.row[i].column[j].single.getName().equals("E")){
                    this.mtx.row[i].column[j].single.setApproachibility(1);
                } else if(this.mtx.row[i].column[j].single.getName().equals("F")){
                    if(j-1 == -1){
                        arr3[0] = this.mtx.row[i-1].column[j].single.getApproachibility();
                        arr3[1] = this.mtx.row[i+1].column[j].single.getApproachibility();
                        arr3[2] = this.mtx.row[i].column[j+1].single.getApproachibility();
                        this.mtx.row[i].column[j].single.setApproachibility(getMinValue(arr3) + 1);
                    } else if(j+1 == 21){
                        arr3[0] = this.mtx.row[i-1].column[j].single.getApproachibility();
                        arr3[1] = this.mtx.row[i+1].column[j].single.getApproachibility();
                        arr3[2] = this.mtx.row[i].column[j-1].single.getApproachibility();
                        this.mtx.row[i].column[j].single.setApproachibility(getMinValue(arr3) + 1);
                    } else{
                        arr4[0] = this.mtx.row[i-1].column[j].single.getApproachibility();
                        arr4[1] = this.mtx.row[i+1].column[j].single.getApproachibility();
                        arr4[2] = this.mtx.row[i].column[j+1].single.getApproachibility();
                        arr4[3] = this.mtx.row[i].column[j-1].single.getApproachibility();
                        this.mtx.row[i].column[j].single.setApproachibility(getMinValue(arr4) + 1);
                    }
                }
            }
        }
    }
    public void ProcessingApprochibility(){
        for(int i = 0; i < 21; i++){
            CalcilateApproachibility();
        }
    }
    public void ChikenPathFinding(){
        int arr4[] = new int[4];
        for(int i = 0; i < 21; i++){
            for(int j = 0; j < 21; j++){
                if(this.mtx.row[i].column[j].single.getName().equals("C") || this.mtx.row[i].column[j].single.getName().equals("W")){
                    arr4[0] = this.mtx.row[i-1].column[j].single.getApproachibility();
                    arr4[1] = this.mtx.row[i+1].column[j].single.getApproachibility();
                    arr4[2] = this.mtx.row[i].column[j+1].single.getApproachibility();
                    arr4[3] = this.mtx.row[i].column[j-1].single.getApproachibility();
                    if(getMinValueIndex(arr4) == 0){
                        this.mtx.row[i-1].column[j].single.setName("W");
                    } else if(getMinValueIndex(arr4) == 1){
                        this.mtx.row[i+1].column[j].single.setName("W");
                    } else if(getMinValueIndex(arr4) == 2){
                        this.mtx.row[i].column[j+1].single.setName("W");
                    } else if(getMinValueIndex(arr4) == 3){
                        this.mtx.row[i].column[j-1].single.setName("W");
                    }
                }
            }
        }
    }
    public void ProcessingPathFinding(){
        while(true){
            try{
                ChikenPathFinding();
            } catch(java.lang.ArrayIndexOutOfBoundsException ex){
                break;
            }
        }
    }
    public static int getMinValue(int[] numbers){
        int minValue = numbers[0];
        for(int i=1;i<numbers.length;i++){
            if(numbers[i] < minValue){
                minValue = numbers[i];
            }
        }
        return minValue;
    }
    public static int getMinValueIndex(int[] numbers){
        int minValue = numbers[0];
        int index = 0;
        for(int i=1;i<numbers.length;i++){
            if(numbers[i] < minValue){
                minValue = numbers[i];
                index = i;
            }
        }
        return index;
    }
    public void printNames(){
        for(int i = 0; i < 21; i++){
            for(int j = 0; j < 21; j++){
                System.out.print(""+mtx.row[i].column[j].single.getName());
            }
            System.out.println();
        }
    }
    public void printApproachibility(){
        for(int i = 0; i < 21; i++){
            for(int j = 0; j < 21; j++){
                System.out.print(" "+mtx.row[i].column[j].single.getApproachibility());
            }
            System.out.println();
        }
    }
}
